package com.ruoyi.project.task.transfer.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.service.ITaskMissionService;
import com.ruoyi.project.task.postpone.domain.TaskPostpone;
import com.ruoyi.project.task.transfer.domain.TaskTransfer;
import com.ruoyi.project.task.transfer.service.ITaskTransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Pathosis
 * @date 2020-09-19 11:51
 * @description: 任务转移前端控制器
 */
@Controller
@RequestMapping("/task/transfer")
public class TaskTransferController extends BaseController{
    private String prefix = "task/transfer";

    @Autowired
    private ITaskTransferService itts;

    @Autowired
    private ITaskMissionService itms;

    @RequiresPermissions("task:transfer:view")
    @GetMapping
    public String toPostpone(){
        return prefix + "/transfer";
    }

    @RequiresPermissions("task:transfer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskTransfer taskTransfer)
    {
        //分页
        startPage();

        List<TaskTransfer> list = itts.selectTaskTransferList(taskTransfer);

        return getDataTable(list);
    }

    @GetMapping("/add")
    public String missionAdd(Model model){
        //查询出所有的员工
        List<PersonInfo> personInfos = itms.selectAllPerson();
        model.addAttribute("list",personInfos );
        TaskMissionVo tmv = new TaskMissionVo();
        //查询所有的延期任务
        List<TaskMissionVo> missionList = itms.selectTaskMissionList(tmv);
        model.addAttribute("missionList", missionList);
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("task:transfer:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TaskTransfer taskTransfer)
    {
        return toAjax(itts.insertTaskTransfer(taskTransfer));
    }

    @RequiresPermissions("task:transfer:remove")
    @Log(title = "任务转移删除", businessType = BusinessType.INSERT)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeSave(String ids)
    {
        try
        {
            return toAjax(itts.deleteTaskPostponeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{transferId}")
    public String edit(@PathVariable("transferId") Long transferId, ModelMap mmap)
    {
        mmap.addAttribute("taskTransfer", itts.selectTaskTransferById(transferId));
        List<PersonInfo> personInfos = itms.selectAllPerson();
        mmap.addAttribute("list",personInfos );
        return prefix + "/edit";
    }


    @RequiresPermissions("task:transfer:edit")
    @Log(title = "任务延期管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskTransfer taskTransfer) {
        return toAjax(itts.updateTaskTransfer(taskTransfer));
    }

}
